package com.block.common.util;



import com.block.common.vo.LottyeryBase;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;


/**
 * 根据概率获取轮播图
 * 
 * 
 *
 */
public class LottyUtil {

	public static  <T extends LottyeryBase> T lottery(List<T>  awards) {
		// 总的概率区间(此处可再优化)
		float totalPro = 0f;
		// 存储每个奖品新的概率区间
		List<Float> proSection = new ArrayList<Float>();
		proSection.add(0f);
		
		// 遍历每个奖品，设置概率区间，总的概率区间为每个概率区间的总和
		for (LottyeryBase award : awards) {
			// 每个概率区间为奖品概率乘以1000（把三位小数转换为整）
		
				totalPro += award.getProbability().floatValue() * 1000;
				proSection.add(totalPro);
			
		}
		// 获取总的概率区间中的随机数
		SecureRandom random = new SecureRandom();
		float randomPro = (float) random.nextInt((int) totalPro);
		// 判断取到的随机数在哪个奖品的概率区间中
		for (int i = 0, size = proSection.size(); i < size; i++) {
			if (randomPro >= proSection.get(i) && randomPro < proSection.get(i + 1)) {
				return awards.get(i);
			}
		}
		return null;
	}
}
